n=int(input())
mass=[]
mass1=[0]
maxi=0
c=0
if 999<n<10000:
    s=str(n)
    n1=int(s[0])
    n2=int(s[1])
    n3=int(s[2])
    n4=int(s[3])
    n5=int(s[0]+s[1])
    n6=int(s[1]+s[2])
    n7=int(s[2]+s[3])
    n8=int(s[0]+s[1]+s[2])
    n9=int(s[1]+s[2]+s[3])
    n10=int(s[0]+s[1]+s[2]+s[3])
    mass=[n1,n2,n3,n4,n5,n6,n7,n8,n9,n10]
    for i in mass:
        for d in range (1,5001):
            if i%d==0:
                c+=1
        if c==2:
            mass1.append(i)
        c=0
    print(max(mass1)) 
else:
    print('0')
